{extend name="base" /}
{block name="body"}
 <form id="export-form" method="post" data-url="{:url('export')}" class="layui-form" >

<blockquote class="layui-elem-quote news_search">
 <div class="layui-inline">
 <ob_link>
 <a id="export" class="layui-btn layui-btn-small" data-url="{:url('export')}"  ><i class="fa fa-cubes"></i>备份数据库</a>
 </ob_link>
</div>
<div class="layui-inline">
 <ob_link><a id="optimize" class="layui-btn layui-btn-small" data-url="{:url('optimize')}"><i class="fa fa-motorcycle"></i>优化表</a> </ob_link>
</div>
<div class="layui-inline">
<ob_link><a id="repair" class="layui-btn layui-btn-small" data-url="{:url('repair')}"><i class="fa fa-wrench"></i>修复表</a> </ob_link>
</div>

</blockquote>


				<div class="layui-box-searchber">
			
	</div>
				<div class="layui-form users_list">
	  	<table class="layui-table">
                        <thead>
                            <tr>
                                <th width="5%" style="text-align:center"><input type="checkbox" name="" lay-skin="primary" lay-filter="allChoose"  id="allChoose"></th>
                                <th width="170">表名</th>
                                <th width="60">数据量</th>
                                <th width="60">引擎</th>
                               
                                <th width="120">数据大小</th>
                                <th width="160">创建时间</th>
                                <th width="160">更新时间</th>
                                <th width="60">状态</th>
                                 <th width="120">操作</th>
                            </tr>
                        </thead>
                      <tbody class="users_content">
                            {volist name='list' id='vo'}
                                <tr>
                                    <td width="5%" style="text-align:center">
                                        <input lay-skin="primary" type="checkbox" lay-filter="choose"  class="subcheck" name="tables[]" value="{$vo.name}">
                                    </td>
                                    <td>{$vo.name}</td>
                                    <td align="center">{$vo.rows}</td>
                                    <td align="center">{$vo.engine}</td>
                                  
                                    <td>{$vo.data_length|format_bytes}</td>
                                    <td align="center">{$vo.create_time}</td>
                                    <td align="center">{$vo.update_time}</td>
                                    <td align="center" id="{$vo.name}" class="info">未备份</td>
                                    <td class="action">
                                <ob_link><a class="ajaxget layui-btn layui-btn-mini"  data-url="{:url('optimize?tables='.$vo['name'])}">
                                <i class="fa fa-motorcycle"></i>优化表
                                </a></ob_link>&nbsp;
                                <ob_link><a class="ajaxget layui-btn layui-btn-mini" data-url="{:url('repair?tables='.$vo['name'])}">
                                <i class="fa fa-wrench"></i>修复表
                                </a></ob_link>
                            </td>
                                </tr>
                            {/volist}
                        </tbody>
                    </table>
                    </div>
                </form>



 {/block}
 {block name="script"}
 <script>
 layui.use(['form'],function(){
	  var form = layui.form
	  ,$ = layui.jquery;
	  
	  var $form = $("#export-form"), $export = $("#export"), tables
      $optimize = $("#optimize"), $repair = $("#repair");

  $optimize.add($repair).click(function(){
	  loading = layer.load(2, {
	      shade: [0.2,'#000']
	    });
	 
	  
      $.post($(this).data("url"), $form.serialize(), function(data){
          if(data.code){
        	  layer.close(loading);
        	  layer.msg(data.msg, {icon: 1, time: 1000}, function(){
        		 
		        });
             
          } else {
        	  layer.close(loading);
        	  layer.msg(data.msg, {icon: 2, anim: 6, time: 1000}, function(){
        		 
		        });
          }
       
      }, "json");
      return false;
  });

  $export.click(function(){
      $export.parent().children().addClass("disabled");
      $export.html("正在发送备份请求...");
      
      $.post(
          $form.data("url"),
          $form.serialize(),
          function(data){
        	
              if(data.code){
                  tables = data.data.tables;
                  $export.html(data.msg + "开始备份，请不要关闭本页面！");
                  backup(data.data.tab);
                
                  window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
              } else {
                
                 
                  layer.msg(data.msg, {icon: 2, anim: 6, time: 1000}, function(){
             		 
  		        });
                  $export.parent().children().removeClass("disabled");
                  $export.html("立即备份");
     
              }
          },
          "json"
      );
      return false;
  });

  function backup(tab, code){
      code && showmsg(tab.id, "开始备份...(0%)");
      $.get( $form.data("url"), tab, function(data){
          if(data.code){
              showmsg(tab.id, data.msg);

              if(!$.isPlainObject(data.data)){
            	
                  $export.parent().children().removeClass("disabled");
                  $export.html("备份完成，点击重新备份");
                  window.onbeforeunload = function(){ return null }
                  return;
              }
              backup(data.data.tab, tab.id != data.data.tab.id);
          } else {
        	  layer.msg(data.msg, {icon: 2, anim: 6, time: 1000}, function(){
          		 
		        });
              $export.parent().children().removeClass("disabled");
              $export.html("立即备份");
        
          }
      }, "json");

  }

  function showmsg(id, msg){
      $form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
  }

	  
 });
 
 </script>
 
  {/block}